//
//  ZCLoginViewController.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/26.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCLoginViewController.h"
#import "ZCQuickLoginView.h"
#import "ZCLoginCollectionCell.h"
#import "ZCLoginInfoModel.h"



@interface ZCLoginViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) UIButton *cancelButton;
@property (strong, nonatomic) UIButton *registerButton;

@property (strong, nonatomic) UICollectionView *loginView;
@property (strong, nonatomic) ZCQuickLoginView *quickLoginView;

@property (strong, nonatomic) NSArray *loginInfoArr;


@end

@implementation ZCLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setupUI{
    

    
    UIImageView *backgroundImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backgroundImage.image = [UIImage imageNamed:@"login_register_background"];
    backgroundImage.userInteractionEnabled = YES;
    self.view = backgroundImage;
    
    //左上角取消按钮
    self.cancelButton = [UIButton buttonWithBackgroundImage:@"login_close_icon" selectedImage:nil title:nil fontSize:0 tintColor:nil action:@selector(cancelButtonAction) target:self cornerRadius:0 highlighted:NO];
    _cancelButton.size = _cancelButton.currentBackgroundImage.size;

    
    //右上角注册按钮
    self.registerButton = [UIButton buttonWithBackgroundImage:nil selectedImage:nil title:@"注册账号" fontSize:15 tintColor:nil action:@selector(registerButtonButtonAction:) target:self cornerRadius:0 highlighted: YES];
    [_registerButton setTitle:@"已有账号?" forState:UIControlStateSelected];
    
    
    
    //登录页面视图
    UICollectionViewFlowLayout *loginFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    loginFlowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 200);
    loginFlowLayout.minimumLineSpacing = 0;
    loginFlowLayout.minimumInteritemSpacing = 0;
    loginFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.loginView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:loginFlowLayout];
    _loginView.dataSource = self;
    _loginView.delegate = self;
    _loginView.backgroundColor = [UIColor clearColor];
    _loginView.bounces = NO;
    [_loginView registerClass:[ZCLoginCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    //快速登录视图
    self.quickLoginView = [[ZCQuickLoginView alloc]init];
//    _quickLoginView.backgroundColor = [UIColor clearColor];
    
    
    
    //添加子控件
    [self.view addSubview:_cancelButton];
    [self.view addSubview:_registerButton];
    [self.view addSubview:_loginView];
    [self.view addSubview:_quickLoginView];
    
    
    //自动布局
    [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(40);
        make.leading.equalTo(self.view).offset(15);
    }];
    
    [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_cancelButton);
        make.trailing.equalTo(self.view).offset(-15);
        make.height.mas_equalTo(16);
        
    }];
    
    [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(_registerButton.mas_bottom).offset(20);
        make.height.mas_equalTo(200);
    }];
    
    [_quickLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.view);
        make.height.mas_equalTo(170);
        
    }];
    
    
}
/**
 *  左上角取消按钮点击事件
 */
-(void)cancelButtonAction{
    ZCLog(@"取消按钮被点击");
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
/**
 *  右上角注册按钮点击事件
 *
 *  @param sender 按钮本身
 */
-(void)registerButtonButtonAction:(UIButton*)sender{
    [self.view endEditing:YES];
    ZCLog(@"注册按钮被点击");
    sender.selected = !sender.selected;
    
    [self.loginView setContentOffset:sender.selected? CGPointMake(self.loginView.width, 0):CGPointMake(0, 0) animated:YES];
}

//修改状态栏样式
-(UIStatusBarStyle)preferredStatusBarStyle{
    
    [super preferredStatusBarStyle];
    
    return UIStatusBarStyleLightContent;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
/**
 *  懒加载数组
 *
 *  @return  数组数据
 */
-(NSArray *)loginInfoArr{
    
    if (_loginInfoArr == nil) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"LoginMessage" ofType:@"plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        
        _loginInfoArr = [ZCLoginInfoModel mj_objectArrayWithKeyValuesArray:array];
        
    }
    return _loginInfoArr;
}

//MARK:UICollectionView的数据源方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.loginInfoArr.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZCLoginCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    
    cell.model = self.loginInfoArr[indexPath.item];
    
    return cell;
}







/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */






@end
